﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class ArapRepository : RepositoryBase<Arap>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">1:应收  2：应付</param>
        /// <param name="status">-1:为审核 1：审核</param>
        /// <param name="financeNumber">发票号</param>
        /// <param name="customerName">客户名称</param>
        /// <returns></returns>
        public IQueryable<ArapView> GetListArapViews(int type, int status, string financeNumber, string customerName)
        {
            var result = from ar in dbcontext.Araps
                         join order in dbcontext.Orders
                             on ar.OrderId equals order.Id
                         join customer in dbcontext.Customers
                             on order.CustomeId equals customer.Id
                         where ar.Type == type
                               && (status == -10 || ar.Status == status)
                               && customer.Name.Contains(customerName)
                               && ar.FinanceNumber.Contains(financeNumber)
                         select new ArapView
                         {
                             Id = ar.Id,
                             OrderId = order.Id,
                             OrderNumber = order.Number,
                             CreateTime = order.CreateTime,
                             CustomerName = customer.Name,
                             Type = ar.Type,
                             Status = ar.Status,
                             ApplyTime = ar.ApplyTime,
                             FinanceNumber = ar.FinanceNumber,
                             TotalMoney = ar.TotalMoney,
                             InvoiceType = ar.InvoiceType
                         };
            return result;
        }

        public List<ArapView> GetListArapYingfuViews(int type, int status, string customerName, string financeNumber)
        {
            var result = from ar in dbcontext.Araps
                         where ar.Type == type
                         && (status == -10 || ar.Status == status)
                         && ar.FinanceNumber.Contains(financeNumber)
                         group ar by ar.FinanceNumber
                         into tempAr
                         select new ArapView
                         {
                             FinanceNumber = tempAr.Key
                         };
            List<ArapView> list = new List<ArapView>();
            foreach (var item in result.ToList())
            {
                ArapView web = new ArapView();
                web.FinanceNumber = item.FinanceNumber;
                var itemobj = dbcontext.Araps.FirstOrDefault(a => a.FinanceNumber == item.FinanceNumber && a.Status == -1);
                if (itemobj != null)
                {
                    web.Status = -1;
                }
                else
                {
                    web.Status = 1;
                }
                list.Add(web);
            }
            /* var result = from ar in dbcontext.Araps
                          join pur in dbcontext.PurchaseComposes
                          on ar.PurchaseComposeId equals pur.Id
                          join customer in dbcontext.Customers
                          on pur.SupplierId equals customer.Id
                          join pro in dbcontext.Products
                          on pur.ProductId equals pro.Id
                          where ar.Type == type
                                && (status == -10 || ar.Status == status)
                                && customer.Name.Contains(customerName)
                                && (ar.FinanceNumber.Contains(financeNumber) || ar.FinanceNumber == null)
                          select new ArapView
                          {
                              Id = ar.Id,
                              CustomerName = customer.Name,
                              Type = ar.Type,
                              Status = ar.Status,
                              ApplyTime = ar.ApplyTime,
                              PurchaseComposeId = ar.PurchaseComposeId.Value,
                              ProductName = pro.Name,
                              FinanceNumber = ar.FinanceNumber
                          };*/
            return list;
        }


        public ArapView GetSingleArapView(Guid id)
        {
            var result = from ar in dbcontext.Araps
                         join pur in dbcontext.PurchaseComposes
                         on ar.PurchaseComposeId equals pur.Id
                         join pr in dbcontext.PurchaseReconciliations
                         on pur.Id equals pr.PurchaseComposeId
                         join customer in dbcontext.Customers
                         on pur.SupplierId equals customer.Id
                         join pro in dbcontext.Products
                         on pur.ProductId equals pro.Id
                         where ar.Id == id
                         select new ArapView
                         {
                             Id = ar.Id,
                             CustomerName = customer.Name,
                             Type = ar.Type,
                             Status = ar.Status,
                             ApplyTime = ar.ApplyTime,
                             PurchaseComposeId = pur.Id,
                             CostPrice = pur.CostPrice,
                             ProductName = pro.Name,
                             FinanceNumber = pr.FinanceNumber,
                             ActualMoney = ar.ActualMoney,
                             ShouldAmount = pur.StandardAmount,
                             ActualAmount = pr.ActualAmount,
                             CreateTimeIsNull = ar.CreateTime,
                             TotalMoney = ar.TotalMoney,
                             ReconciliationType = pr.ReconciliationType
                         };
            return result.FirstOrDefault();
        }
    }
}
